﻿@{
    Layout = "~/Views/Shared/_Form.cshtml";
}
@using Raffles.OA.Data;
@using Raffles.OA.Utility;
<style>
    .font{
        font-size:15px;
    }
    .table tr{
        text-align:center;
    }
    .table thead tr td {
        font-weight:bold;
    }
    .form-control {
        background-color: #FAFAD2;
    }
</style>
<form id="form" class="form-horizontal m" role="form">
    <div class="form-group">
        <div class="col-sm-12">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th colspan="9" class="title text-center text-bold text-red font"></th>
                    </tr>
                    <tr>
                        <td width="100px">天数</td>
                        <td width="150px">日期</td>
                        <td>使用时间</td>
                        <td>计划用时</td>
                        <td>超时数</td>
                        <td>接/送($)</td>
                        <td>工资($)</td>
                        <td>超时费($)</td>
                        <td width="150px">小计($)</td>
                    </tr>
                </thead>
                <tbody id="trip_body">
                </tbody>
            </table>
        </div>
    </div>
</form>
<script>moment.locale('zh-cn');</script>
<script type="text/javascript">
    var itemId = parseInt(frame.request("id")) || 0;
    var segmentId = parseInt(frame.request("segmentId"));
    var code = frame.request("code");
    var projectId = frame.request("projectId");
    var title = frame.request('title');
    var priceId = frame.request("priceId");
    var dateGo = frame.request('startDate');
    var days = frame.request('days');
    
    $(function () {
        $('.title').text(title); 
        let filterData = parent.PageUI.initValue.QuotationTransportDetail.filter(x => x.Code == code && x.SegmentId == segmentId && x.ItemId == itemId);
        if (!!days && days > 0) {
            for (var i = 0; i < days; i++) {
                renderDays(dateGo, i, days, (filterData.length == 0 ? null : filterData[i]))
            }
            var html = [];
            html.push("<tr>");
            html.push("<td colspan=\"8\" style=\"text-align:right\">");
            html.push("<span class=\"text-bold text-green font\">合计：</span>");
            html.push("</td>");
            html.push("<td>");
            html.push("<span class=\"text-bold text-green font\" id=\"sumFee\">0</span>");
            html.push("</td>");
            html.push("</tr>");
            $("#trip_body").append(html.join('')); 
            //主动触发文本框input事件
            $('#trip_body input[name="fee"]').trigger('input');
        }
    });
    function renderDays(dateGo, i, days, filterData) { 
        var html = [];
        html.push("<tr>");

        html.push("<td>");
        html.push(`第${padWithZero(i + 1, 2)}天`);
        html.push("</td>");

        html.push("<td>");
        html.push(`${moment(dateGo).add(i, 'days').format('YYYY.MM.DD dddd')}`);
        html.push("</td>");

        html.push("<td>");
        html.push("<input type=\"text\" col=\"UseTime\" class=\"form-control text-center\" value=\"" + (filterData == null ? '' : filterData.UseTime ?? '') + "\" />");
        html.push("</td>");

        html.push("<td>");
        html.push("<input type=\"text\" col=\"PlanHour\" class=\"form-control text-center\" value=\"" + (filterData == null ? '' : filterData.PlanHour ?? '') + "\" />");
        html.push("</td>");

        html.push("<td>");
        html.push("<input type=\"text\" col=\"OverTime\" class=\"form-control text-center\" value=\"" + (filterData == null ? '' : filterData.OverTime ?? '') + "\" />");
        html.push("</td>");

        html.push("<td>");
        html.push("<input type=\"number\" col=\"ServiceFee\" name=\"fee\" class=\"form-control text-center\" oninput=\"inputEvent($(this))\" value=\"" + (filterData == null ? '' : filterData.ServiceFee) + "\" />");
        html.push("</td>");

        html.push("<td>");
        html.push("<input type=\"number\" col=\"SalaryFee\" name=\"fee\" class=\"form-control text-center\" oninput=\"inputEvent($(this))\" value=\"" + (filterData == null ? '' : filterData.SalaryFee) + "\" />");
        html.push("</td>");

        html.push("<td>");
        html.push("<input type=\"number\" col=\"OverTimeFee\" name=\"fee\" class=\"form-control text-center\" oninput=\"inputEvent($(this))\" value=\"" + (filterData == null ? '' : filterData.OverTimeFee) + "\" />");
        html.push("</td>");

        html.push("<td>");
        html.push("<span name=\"feeTotal\" col=\"TotalFee\" class=\"font\"></span>");
        html.push("</td>");

        html.push("</tr>");

        $("#trip_body").append(html.join(''));
    }

    //计算小计
    function inputEvent($this) {
        //单项合计
        var totalFee = $this.parent().parent().find('input[name=fee]').map(function () {
            return parseFloat($(this).val()) || 0;
        }).get().reduce(function (accumulator, currentValue) {
            return accumulator + currentValue;
        }, 0);
        $this.parent().parent().find('span[name=feeTotal]').text(totalFee);

        //汇总合计
        var sumFee = $('#trip_body span[name="feeTotal"]').map(function () {
            return parseFloat($(this).text()) || 0;
        }).get().reduce(function (accumulator, currentValue) {
            return accumulator + currentValue;
        }, 0); // 初始累加值为0
        $('#sumFee').text(sumFee);
    };

    function saveForm(index) {
        parent.PageUI.initValue.QuotationTransportDetail = parent.PageUI.initValue.QuotationTransportDetail.filter(x => !(x.Code == code && x.SegmentId == segmentId && x.ItemId == itemId));//筛选出非当前的明细赋值给当前数据  实现移除原数据的效果
        $('#trip_body tr:not(:last)').each(function (i) {
            parent.PageUI.initValue.QuotationTransportDetail.push(
                {
                    Code: code,
                    SegmentId: segmentId,
                    ItemId: itemId,//下拉选项Id
                    Day: i + 1,
                    UseTime: $(this).find('input[col=UseTime]').val(),
                    PlanHour: $(this).find('input[col=PlanHour]').val(),
                    OverTime: $(this).find('input[col=OverTime]').val(),
                    ServiceFee: $(this).find('input[col=ServiceFee]').val(),
                    SalaryFee: $(this).find('input[col=SalaryFee]').val(),
                    OverTimeFee: $(this).find('input[col=OverTimeFee]').val(),
                    TotalFee: $(this).find('span[col=TotalFee]').text()
                });
        }); 
        var total = parseFloat($('#sumFee').text()) || 0;
        parent.document.getElementById(priceId).value = total.toFixed(2);
        parent.PageUI.triggerForPageInputEvent(priceId);
        parent.layer.close(index);
    }

    function padWithZero(num, fill) {
        let len = ('' + num).length;
        if (fill > len) {
            num = Array(fill - len + 1 || 0).join("0") + num;
        }
        return num;
    }
</script>